import DB from "@/core/db/sqlite.js";

const sqobj = {
  data() {
    return {
      listData: [],
    };
  },
  methods: {
    // 打开数据库
    openSQL() {
      // 这个是查询有没有打开数据库
      let open = DB.isOpen();
      console.log("数据库状态", open ? "开启" : "关闭");
      if (!open) {
        DB.openSqlite()
          .then((res) => {
            console.log("数据库已打开");
          })
          .catch((error) => {
            console.log("数据库开启失败");
          });
      }
    },
    // 关闭数据库
    closeSQL() {
      // 这个是查询有没有打开数据库
      let open = DB.isOpen();
      if (open) {
        DB.closeSqlite()
          .then((res) => {
            console.log("数据库已关闭");
          })
          .catch((error) => {
            console.log("数据库关闭失败");
          });
      }
    },
    // 创建表
    createTable(tableName, sql) {
      console.log('创建表', tableName)
      let open = DB.isOpen();
      if (open) {
        this.openSQL();
        // let sql = '"id" INTEGER PRIMARY KEY AUTOINCREMENT,"name" text,"content" text,"time" text';
        // 创建表 DB.createTable(表名, 表的列)
        DB.createTable(tableName, sql)
          .then((res) => {
            console.log(`创建${tableName}表成功`);
            this.showToast(`创建${tableName}表成功`);
          })
          .catch((error) => {
            console.log("创建表失败");
          });
      } else {
        console.log("数据库未打开");
      }
    },
    // 删除表
    dropTable(tableName){
      let open = DB.isOpen();
      if (open) {
        // 删除表 DB.dropTable(表名)
        DB.dropTable(tableName)
          .then((res) => {
            console.log(`删除${tableName}表成功`);
          })
          .catch((error) => {
            console.log("删除表失败");
          });
      } else {
        console.log("数据库未打开");
      }
    },
    // 新增表数据
    insertTableData(tableName, sql, condition) {
    
   
      let open = DB.isOpen();
      if (open) {
       
          //   let time = this.formatDate(new Date().getTime());
          //   let sql = `'${item.name}','${item.content}','${time}'`;
          //   let condition = "'name','content','time'";
          // 新增 DB.insertTableData(表名, 对应表头列的数据)
          DB.insertOrReplaceData(tableName, sql, condition)
            .then((res) => {
              console.log("新增数据成功");
          
              this.selectTableData(tableName);
            })
            .catch((error) => {
              console.log("失败", error);
            });
      
      } else {
        console.log("数据库未打开");
      }
    },
    // 查询表数据
    selectTableData(tableName, key, value) {
      let open = DB.isOpen();
      if (open) {
        // 查询表 DB.selectTableData(表名,查询条件列名,查询条件列值)
        console.log('查询表数据', tableName, key, value)
        if (key && value) {
          return new Promise((resolve, reject) => {
            DB.selectTableData(tableName, key, value)
              .then((res) => {
                console.log(`按照条件${key}查询的${tableName}表数据`, res);
                resolve(res)
              })
              .catch((error) => {
                console.log("查询失败", error);
                reject(error)
              });
          });
        } else {
          DB.selectTableData(tableName)
            .then((res) => {
              console.log(`${tableName}表数据`, res);
            })
            .catch((error) => {
              console.log("查询失败", error);
            });
        }
      } else {
        console.log("数据库未打开");
      }
    },
    // 分页查询表数据
    selectTableDataByPage(tableName, page, size, key, value) {
      let open = DB.isOpen();
      if (open) {
        // 分页查询表 DB.selectTableDataByPage(表名,查询条件列名,查询条件列值,页码,每页条数)
        if (key && value) {
          DB.pullSQL(tableName, key, value, page, size)
            .then((res) => {
              console.log(`分页按照条件${key}查询的${tableName}表数据`, res);
              this.listData = res;
            })
            .catch((error) => {
              console.log("查询失败", error);
            });
        } else {
          DB.pullSQL(tableName, page, size)
            .then((res) => {
              console.log(`分页查询${tableName}表数据`, res);
              this.listData = res;
            })
            .catch((error) => {
              console.log("查询失败", error);
            });
        }
      } else {
        console.log("数据库未打开");
      }
    },
    // 删除表数据
    deleteTableData(tableName, key, value) {
      let open = DB.isOpen();
      if (open) {
        // 删除表 DB.deleteTableData(表名,查询条件列名,查询条件列值)
        if (key && value) {
          DB.deleteTableData(tableName, key, value)
            .then((res) => {
              console.log("删除表数据成功");
              // this.selectTableData(tableName);
            })
            .catch((error) => {
              console.log("删除失败", error);
            });
        } else {
          DB.deleteTableData(tableName)
            .then((res) => {
              console.log("删除表数据成功");
              // this.selectTableData(tableName);
            })
            .catch((error) => {
              console.log("删除失败", error);
            });
        }
      } else {
        console.log("数据库未打开");
      }
    },
    // 提示框
    showToast: function (str) {
      uni.showToast({
        icon: "none",
        title: str,
        mask: true,
      });
    },
    // 时间戳转年月日
    formatDate(data) {
      let now = new Date(data);
      var year = now.getFullYear(); //取得4位数的年份
      var month =
          now.getMonth() + 1 < 10 ?
              "0" + (now.getMonth() + 1) :
              now.getMonth() + 1;
      var date = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
      var hour = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
      var minute =
          now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
      var second =
          now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds();
      return (
          year +
          "-" +
          month +
          "-" +
          date +
          " " +
          hour +
          ":" +
          minute +
          ":" +
          second
      );
    }
  },
};

export default sqobj;
